package com.scalar.chiptrack.wipProcessView;


import java.io.Serializable;
import java.util.ArrayList;
import java.awt.*;

/**
 * User: Sujan
 * Date: May 3, 2005
 * Time: 1:04:14 PM
 */
public class Node implements Serializable
{
	private int id;
	private int level 					= -1;
	public NodeList nodeList;
	public EdgeList edgeList;
	private boolean isFixed 			= false;
	private int parent 					= -1;
	public int x 						= -1;
	public int absX 					= -1;
	public int y 						= -1;
	private int width					= 150;
	private int height 					= 85;
	private int relativeParent 			= -1;
	private String partNumber 			= null;
	private String psType 				= null;
	private String psTypeName 			= null;
	private int wipTotalQty 			= 0;
	private ArrayList lotList 			= new ArrayList();
	private final int nodeLabelMaxChars = 19;     //maximum chars per line
	public Node(int id, NodeList nodeList, EdgeList edgeList)
	{
		this.id = id;
        this.nodeList = nodeList;
        this.edgeList = edgeList;
    }
	public int getId ()
	{
		return id;
	}
	public void setNodeList( NodeList nodeList )
	{
		this.nodeList = nodeList;
	}
	public NodeList getNodeList ()
	{
		return nodeList;
	}

	public boolean isFixed ()
	{
		return isFixed;
	}

	public void setFixed ( boolean fixed )
	{
		isFixed = fixed;
	}

	public EdgeList getEdgeList ()
	{
		return edgeList;
	}

	public void setEdgeList ( EdgeList edgeList )
	{
		this.edgeList = edgeList;
	}
	public int getNodeIndex ( Node node )
	{
		return nodeList.indexOf ( node );
	}
	public int getIndex ( )
	{
		return nodeList.indexOf ( this );
	}
	public NodeList getChilds ()
	{
		NodeList childList = new NodeList();
		int nodeIndex = getIndex( );
		int edgeListSize = edgeList.size ();
		com.scalar.chiptrack.wipProcessView.Edge temp = null;
		for ( int i = 0; i < edgeListSize; i++ )
		{
			temp = ( com.scalar.chiptrack.wipProcessView.Edge )edgeList.get ( i );
			if ( temp.getFrom () == nodeIndex )
			{
				childList.add ( nodeList.get ( temp.getTo() ) );
			}
		}
		return childList;
	}
	public NodeList getChilds ( Node node )
	{
		NodeList childList = new NodeList();
		int nodeIndex = getNodeIndex( node );
		int edgeListSize = edgeList.size ();
		com.scalar.chiptrack.wipProcessView.Edge temp = null;
		for ( int i = 0; i < edgeListSize; i++ )
		{
			temp = ( Edge )edgeList.get ( i );
			if ( temp.getFrom () == nodeIndex )
			{
				childList.add ( nodeList.get ( temp.getTo() ) );
			}
		}
		return ( childList.size() > 0 ) ? childList : null;
	}
	public Node getLeftChild ()
	{
		Node leftChild = null;
		NodeList nodeList = getChilds();
		for ( int i=0; i < nodeList.size(); i++ )
		{
			Node node = ( Node )nodeList.get ( i );
			if ( node.getParentIndex () < 0 )
			{
				leftChild = node;
				break;
			}
			else
			{
				if ( node.getParentIndex () == this.getIndex () )
				{
					leftChild = node;
					break;
				}
			}
		}

		return leftChild;
	}
	public void setParent ( Node node )
	{
		this.parent = getNodeIndex ( node );
	}
	public void setParent ( int parentIndex )
	{
		this.parent = parentIndex;
	}
	public Node getParent ( )
	{
		return ( parent >=0 ) ? ( Node )nodeList.get ( parent ) : null;
	}
	public int getParentIndex ()
	{
		return parent;
	}
	public Node getRightSibling ( )
	{
		NodeList childList = getChilds ( getParent () );
		Node rightSibling = null;
		if ( childList != null )
		{
			int childListSize = childList.size();
			int idx = childList.indexOf ( this );
			for ( int i=idx+1; i < childListSize ; i++ )
			{
				Node node = ( Node )childList.get ( i );
				if ( node.getParentIndex () < 0 )
				{
					rightSibling = node;
					break;
				}
				else
				{
					if ( node.getParentIndex () == this.parent )
					{
						rightSibling = node;
						break;
					}
				}
			}
		}
		return rightSibling;
	}

	public Node getLeftSibling ( )
	{
		NodeList childList = getChilds ( getParent () );
		Node leftSibling = null;
		if ( childList != null )
		{
			//int childListSize = childList.size();
			int idx = childList.indexOf ( this );
			for ( int i=idx-1; i >= 0 ; i-- )
			{
				Node node = ( Node )childList.get ( i );
				if ( node.getParentIndex () < 0 )
				{
					leftSibling = node;
					break;
				}
				else
				{
					if ( node.getParentIndex () == this.parent )
					{
						leftSibling = node;
						break;
					}
				}
			}
		}
		return leftSibling;
	}
	public int getLevel ()
	{
		return level;
	}

	public void setLevel ( int level )
	{
		this.level = level;
	}
    public Rectangle getBounds ()
	{
		return new Rectangle ( absX, y, width, height );
	}

	public int getWidth ()
	{
		return width;
	}

	public void setWidth ( int width )
	{
		this.width = width;
	}

	public int getHeight ()
	{
		return height;
	}

	public void setHeight ( int height )
	{
		this.height = height;
	}

	public Node getRelativeParent ()
	{
		return ( relativeParent >=0 ) ? ( Node )nodeList.get ( relativeParent ) : null;
	}
    public int getRelativeParentIndex ()
	{
		return relativeParent;
	}
	public void setRelativeParent ( int relativeParent )
	{
		this.relativeParent = relativeParent;
	}
	public void setRelativeParent ( Node relativeParent )
	{
		this.relativeParent = getNodeIndex ( relativeParent );
	}
    public int getAbsX ()
	{
		int tempAbsX = -1;
		if ( this.absX < 0 )
		{
			tempAbsX = this.x + getRelativeParent ( ).absX;
		}
		else
		{
			tempAbsX = this.absX;
		}
		return tempAbsX;
	}

	public String getPartNumber ()
	{
		return partNumber;
	}

	public void setPartNumber ( String partNumber )
	{
		this.partNumber = partNumber;
	}

	public String getPsType ()
	{
		return psType;
	}

	public void setPsType ( String psType )
	{
		this.psType = psType;
	}

	public String getPsTypeName ()
	{
		return psTypeName;
	}

	public void setPsTypeName ( String psTypeName )
	{
		this.psTypeName = psTypeName;
	}

	public int getWipTotalQty ()
	{
		return wipTotalQty;
	}

	public void setWipTotalQty ( int wipTotalQty )
	{
		this.wipTotalQty = wipTotalQty;
	}

	public ArrayList getLotList ()
	{
		return lotList;
	}

	public void setLotList ( ArrayList lotList )
	{
		this.lotList = lotList;
	}
    public void addLot ( Object lot )
	{
		this.lotList.add ( lot );
	}
	public String getNodeLabel ()
	{
		StringBuffer label = new StringBuffer();
		label.append ( "<html>" );
		label.append ( "\n" );
		label.append ( psType );
		label.append ( "<br>" );
		label.append ( "\n" );
		boolean addBR = false;
		int partNumberLength = partNumber.length();
		for ( int i=1; i <= partNumberLength/nodeLabelMaxChars; i++ )
		{
			int startIndex 	= ( i-1)*nodeLabelMaxChars ;
			int endIndex 	= i*nodeLabelMaxChars;
			if ( i == 1 )
			{
				label.append ( partNumber.substring ( startIndex, endIndex ) );
			}
			else
			{
				label.append ( "<br>" );
				label.append ( partNumber.substring ( startIndex, endIndex ) );
			}
			addBR = true;
		}
		if ( ( partNumberLength % nodeLabelMaxChars ) > 0 )
		{
			if ( addBR )
			{
				label.append ( "<br>" );
			}
			label.append ( partNumber.substring ( partNumberLength - ( partNumberLength % nodeLabelMaxChars ), partNumberLength ) );
		}
		label.append ( "<br>" );
		label.append ( "\n" );
		label.append ( "WIP: " + wipTotalQty );
		label.append ( "</html>" );
		return label.toString ();
	}
	public String getLotInfo ( )
	{
		StringBuffer lotInfo = new StringBuffer ();
		LotInfo lot = null;
		lotInfo.append ( "<html>\n" );
		int lotListSize = lotList.size();
		if ( lotListSize == 0 )
		{
			lotInfo.append ( "Lot(s) Not Found" );
			lotInfo.append ( "\n" );
		}
		else
		{
            lotInfo.append ( "<table>\n" );
			for ( int i = 0; i < lotListSize; i++ )
			{
				lot = ( LotInfo )lotList.get ( i );
				lotInfo.append ( "<TR>\n" );
				lotInfo.append ( "<td>\n" );
                lotInfo.append ( "<font color='red'>\n" );
				if ( lot.isHold () )
				{
					lotInfo.append ( "*" );
				}
				else
				{
					lotInfo.append ( "&nbsp;" );
				}
                lotInfo.append ( "</font>\n");
                lotInfo.append ( "</td>\n" );
				lotInfo.append ( "<td>\n" );
                lotInfo.append ( lot.getLotNumber ()+"\n" );
				lotInfo.append ( "</td>\n" );
                lotInfo.append ( "<td>\n" );
				lotInfo.append ( lot.getFabLotNumber()+"\n" );
				lotInfo.append ( "</td>\n" );
                lotInfo.append ( "<td>\n" );
				lotInfo.append ( lot.getLocationName ()+"\n" );
				lotInfo.append ( "</td>\n" );
				lotInfo.append ( "<td>\n" );
				lotInfo.append ( lot.getQuantityIn () + "\n" );
				lotInfo.append ( "</td>\n" );
                lotInfo.append ( "<td>\n" );
				lotInfo.append ( lot.getLotCategory ()+"\n" );
				lotInfo.append ( "</td>\n" );
                lotInfo.append ( "</TR>\n" );
			}
			lotInfo.append ( "</table>\n" );
        }
		return lotInfo.toString ();
	}
	public String toString ()
	{
		return "Node :" + id + " level:" + level;
	}
}
